package com.lcb.blur;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @author lcb
 * @date 2021-06-11 11:16
 */
public class BlurView extends View {

    private Paint paint;

    public BlurView(Context context) {
        this(context, null);
    }

    public BlurView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BlurView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.LTGRAY);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setMaskFilter(new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER));

        //关闭硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(getWidth() / 4f, getHeight() / 2f, 40, paint);
        //INNER: 内部模糊，外部不绘制
        canvas.drawRect(30, 30, 130, 130, paint);
    }
}